package com.sandau.basic.controller;

import com.sandau.basic.global.GlobalEnum;
import com.sandau.basic.service.UserService;
import com.sandau.basic.vo.BasicMsg;
import com.sandau.basic.vo.UserVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
@Tag(name="User")
public class UserController {
    @Resource
    private UserService userService;

    @GetMapping("/users")
    @Operation(summary = "Add a new base.")
    public String getUsers() {
        return "get users";
    }

    @PostMapping("/users")
    @Operation(summary = "Add a new user.")
    public BasicMsg addUser(@RequestBody UserVo user){
        BasicMsg msg = new BasicMsg();
        GlobalEnum.ErrorCode errorCode = userService.addUser(user);
        msg.setErrorCode(errorCode);
        if(errorCode == GlobalEnum.ErrorCode.OK){
            msg.setErrorMsg("添加用户成功。");
            msg.setOperateResult(GlobalEnum.OperateResult.SUCCESS);
        }else{
            msg.setErrorMsg("添加用户失败。");
            msg.setOperateResult(GlobalEnum.OperateResult.FAIL);
        }
        return msg;
    }
}

